﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO.Ports;
using XBee.Communications;
using XBee.Library;

namespace Windows.XBee.Test
{
    public partial class frmPpal : Form
    {
        XBEE _xbee;

        public frmPpal()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            _xbee = new XBEE(cmbPorts.SelectedItem.ToString(), new TestData());
            _xbee.OnReciveData += new ReciveDataDelegate(_xbee_OnReciveData);
        }

        void _xbee_OnReciveData(ICommunicationData data)
        {
            this.rtbLog.Invoke(new MethodInvoker(delegate() { this.rtbLog.Text += data.Serialize() + Environment.NewLine; }));            
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            _xbee.Write(new TestData(rdbOn.Checked));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cmbPorts.Items.AddRange(SerialPort.GetPortNames());
        }

        private void btnClearAll_Click(object sender, EventArgs e)
        {
            this.rtbLog.Clear();
        }
    }
}
